﻿var _postBeginsWith = '<div class=\'post-body';
var _postEndsWith = '<div class=\'post-footer';
var _paragraphTab = '[color=#404040]· · · · [/color]';
var _paragraphSpace = '[color=#404040][size=1]\r\n·[/size][/color]';
var _mapDiv = '<div class="map">';
var _blogUrl = '';
var _blogID = '118459106898417641';

// http://www.blogger.com/feeds/01593037958345853300/blogs
// 118459106898417641
// www.blogger.com/feeds/118459106898417641/posts/default

// http://code.google.com/chrome/extensions/api_other.html

addBlogControl();

function addBlogControl() {
	var button = document.createElement('input'); 
	var list = document.createElement('select');
	var td = document.createElement('td');
	
	td.className = 'blog-control';
	list.className = 'post-list';
	button.className = 'blog-button';
	button.setAttribute('type','button');
	button.setAttribute('value','Insert Post');
	
	button.addEventListener('click', loadPost, false);
	
	td.appendChild(list);
	td.appendChild(button);
	
	document
		.getElementById('vB_Editor_001_controls')
		.getElementsByTagName('tr')[0]
		.appendChild(td);

	chrome.extension.sendRequest(
		{'action': 'getList', 'id': _blogID },
		function(data) {
			if (!data) { alert('unable to load blog ' + _blogID); return; }
			
			for (var i = 0; i < data.length; i++) {
				var option = document.createElement('option');
				option.text = data[i].title;
				option.value = data[i].url;
				list.add(option, null);
			}
		}
	);
	
	function loadPost(e) {
		chrome.extension.sendRequest({
			'action': 'getPost',
			'id': list.options[list.selectedIndex].value
		}, onPostLoad);
	}
}
	
function onPostLoad(data) {
	// roughly narrowed down
	var text = data.substring(data.indexOf(_postBeginsWith), data.indexOf(_postEndsWith));

	// specifically narrowed down
	var start = text.indexOf('<p>');
	var quoteStart = text.indexOf('<blockquote>');
	if (quoteStart >= 0 && quoteStart < start) { start = quoteStart; }
	var end = text.lastIndexOf('</p>');
	text = text.substr(start, (end - start) + 4);
	
	text = text.replace(/<em>([\w\s]+)<\/em>/gi, '[i]$1[/i]');
	text = text.replace(/<strong>([\w\s]+)<\/strong>/gi, '[b]$1[/b]');
	text = replaceSpecialCharacters(text);
	
	// block quote
	text = text.replace('<blockquote>', '[indent][indent][size=-1]');
	text = text.replace('</blockquote>', '[/size][/indent][/indent]');
	
	// paragraphs
	//text = Regex.Replace(text, @'</p>\s*<p>', '</p>' + _paragraphSpace + '<p>');
	text = text.replace(/\s*<\/p>\s*/gi, '[/indent]');
	text = text.replace(/\s*<p>\s*/gi, '[indent]' + _paragraphTab);
	
	// expand relative links
	text = text.replace('href="', 'href="' + _blogUrl + '/');
	
	// link
	text = text.replace(/<a href="([^"]+)">([^<]+)<\/a>/gi, '[url=$1]$2[/url]');
	// image
	// <div class="photo"><img alt="5569477742" src="http://farm6.static.flickr.com/5026/5569477742_a98f2efbce_b.jpg" /></div>
	text = text.replace(
		/<div class=['"]photo['"]><img[alt='"\d\s]+src=["']([^"']+)["'][alt='"\d\s]*\/><\/div>/gi,
		'\r\n\r\n[center][img]$1[/img][/center]\r\n');
	// image with caption
	text = text.replace(
		/<div class=["']photo["']><img[alt="'\d\s]+src=['"]([^"']+)['"'[alt='"\d\s]*\/>\s*<div>([^<]+)\s*<\/div>\s*<\/div>/gi,
		'\r\n[center][img]$1[/img]\r\n[font=Tahoma][size=-1][b]$2[/b][/size][/font][/center]\r\n');
	
	// map
	start = text.indexOf(_mapDiv);
	if (start > 0) {
		text = text.Replace(_mapDiv, '[center]');
		end = text.indexOf('</div>', start);
		text = text.substring(0, end) + '[/center]' + text.substring(end + 6);
	}
	text = '[font=Georgia, Times New Roman][size=+1]' + text + '[/size][/font]';
	
	document.getElementById('vB_Editor_001_textarea').innerHTML = text;
}


function replaceSpecialCharacters(text) {
	text = text.replace('&#8217;', '’');
	text = text.replace('&#8230;', '…');
	text = text.replace('&#8220;', '“');
	text = text.replace('&#8221;', '”');
	text = text.replace('&#8212;', '—');
	text = text.replace('&amp;', '&');
	text = text.replace('&#160;', ' ');
	return text;
}